package com.topsun.framework;

import com.alibaba.fastjson2.JSON;
import com.topsun.framework.common.kafka.consumer.DefaultConsumer;
import com.topsun.framework.common.kafka.pojo.Message;
import com.topsun.framework.common.kafka.producer.DefaultProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;

@Slf4j
@Configuration
public class TestConfig {

    @Bean
    public TestProducer testProducer(KafkaTemplate kafkaTemplate) {
        TestProducer testProducer = new TestProducer();
        testProducer.setKafkaTemplate(kafkaTemplate);
        return testProducer;
    }


    @Bean
    public DefaultConsumer<String, String> defaultConsumer() {
        return new TestConsumer();
    }

    public static class TestProducer extends DefaultProducer<String, String> {
        @Override
        public String getTopic() {
            return "test";
        }
    }

    @KafkaListener(id = "testConsumer", topics = "test")
    public static class TestConsumer extends DefaultConsumer<String, String> {

        @Override
        protected boolean processMessage(Message<String, String> data) {
            log.info(JSON.toJSONString(data));
            return true;
        }
    }
}
